import { defineStore } from "pinia";
import { IMenu } from "#/menu";
import router from "@/router"
import { RouteLocationNormalized, RouteLocationNormalizedLoaded, RouteRecordRaw } from "vue-router";
import { CacheEnum } from "@/enum/userEnum";
import utils from "@/utils";
import store from "@/utils/store";
export  default defineStore("menu",{
    state:()=>{ //data
        return {
            menus:[] as IMenu[],
            historyMenu:[] as IMenu[],
            isClose:store.get(CacheEnum.MENU_IS_CLOSE)??true as boolean ,//默认菜单不关闭
            route:null as  null | RouteLocationNormalized
        }
    },
    actions:{
     init(){
        this.getMenuByRoute();
       this.historyMenu=this.getHistoryMenu();
      },

      //获取历史菜单
      getHistoryMenu(){
        const routes =[] as RouteRecordRaw []
        router.getRoutes().map(r=>routes.push(...r.children))
       let menus:IMenu[]= utils.store.get(CacheEnum.HISTORY_MENU)??[]
       return menus.filter(m=>{
         return    routes.some(r=>r.name==m.route)
       })
      },
    
      //移除历史菜单
       removeHistoryMenu(menu:IMenu){
         const index=   this.historyMenu.indexOf(menu)
            this.historyMenu.splice(index,1)
            utils.store.set(CacheEnum.HISTORY_MENU,this.historyMenu)

      },
        //根据路由获取菜单
        getMenuByRoute(){
            this.menus =  router
            .getRoutes()
            .filter(route => route.children.length && route.meta.menu)
            .map(route => {
              let menu: IMenu = { ...route.meta?.menu }
              menu.children = route.children
                .filter(route => route.meta?.menu)
                .map(route => {
                  return { ...route.meta?.menu, route: route.name }
                }) as IMenu[]
              return menu
            })
            .filter(menu => menu.children?.length) as IMenu[]
        },

         //添加历史菜单
        addHistoryMenu(route:RouteLocationNormalized){
          if(!route.meta?.menu) return ; //菜单之外的选项不用记录

          this.route=route;

          const menu:IMenu ={...route.meta?.menu,route:route.name as string}
          //去重
          const isHas =this.historyMenu.some(menu=>menu.route==route.name)
          if(!isHas){
            this.historyMenu.unshift(menu)
          }
          if(this.historyMenu.length>10){ 
            this.historyMenu.pop()
          }
          //每次点击每一个菜单，加入到缓存，防止刷新就没有了(init读取)
          utils.store.set(CacheEnum.HISTORY_MENU,this.historyMenu)
        },


        setCurrentMenu(route:RouteLocationNormalizedLoaded){
           this.menus.forEach(m=>{
             m.isClick=false
             m.children?.forEach(c=>{
              c.isClick=false
             if(c.route==route.name){
              m.isClick=true
              c.isClick=true
             }
             })
           })
        },

        toggleState(){
           this.isClose=!this.isClose
           store.set(CacheEnum.MENU_IS_CLOSE,this.isClose)
        },

        //菜单的点击
        toggleParentMenu(menus:IMenu){  
          this.menus.forEach((m) => {
            m.isClick = false 
            if(m==menus){
                m.isClick=true
            }
          })
        
        },


  
}
,  persist:{
  key:'menustore',
  storage:sessionStorage
}



    }
 
)



